package utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.*;
import java.util.Properties;

public class DruidUtils {
    //连接池
    public static DataSource ds;
    //静态代码块加载配置文件
    static {
        Properties pro = new Properties();
        try {
            pro.load(DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //获取连接方法
    public static Connection getConnection(){
        Connection con;
        try{
            con = ds.getConnection();
            return con;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //关闭连接
    public static void close(Connection con, Statement sta){
        JDBCUtils.close(con,sta);
    }
    //关闭连接 + 结果集
    public static void close(Connection con,Statement sta,ResultSet rs){
        JDBCUtils.close(con, sta, rs);
    }
}
